Ext.ns("App");

//首页PORTAL元素
var PortalItem=function(panelId,column,row){
   this.panelId=panelId;
   this.column=column;
   this.row=row;
};
//用户信息
var UserInfo=function(userId,isRestAdmin,isOfficeManager,username,fullname,depId,depName,rights,conf){
	this.userId=userId;
	this.fullname=fullname;
	this.depId=depId;
	this.username = username;
	this.depName=depName;
	this.isRestAdmin = isRestAdmin; // 是否是食堂管理员角色 By F3220847
	this.isOfficeManager = isOfficeManager;//是否是室经理 
	this.rights=rights;
	this.portalConfig=conf;
};

//当前登录用户
var curUserInfo=null;

//检查当前用户有权访问funKey对应的功能
function isGranted(funKey){
	if(curUserInfo.rights.indexOf('__ALL')!=-1){
		return true;
	}
	if(curUserInfo.rights.indexOf(funKey)!=-1){
		return true;
	}
	return false;
}

App.init = function() {
	Ext.QuickTips.init();//这句为表单验证必需的代码
    //Ext.form.Field.prototype.msgTarget = "side" ;
	Ext.BLANK_IMAGE_URL=__ctxPath+'/ext3/resources/images/default/s.gif';
	setTimeout(function() {
				Ext.get('loading').remove();
				Ext.get('loading-mask').fadeOut({remove:true});
				document.getElementById('app-header').style.display='block';
			}, 1000); 
	
	Ext.util.Observable.observeClass(Ext.data.Connection);
	Ext.data.Connection.on('requestcomplete', function(conn, resp,options ){
		if (resp && resp.getResponseHeader){
		    if(resp.getResponseHeader('__timeout')) {
		    	Ext.ux.Toast.msg('操作提示：','操作已经超时，请重新登录!');
	        	window.location.href=__ctxPath+'/index.jsp?randId=' + parseInt(1000*Math.random());
	    	}
	    	if(resp.getResponseHeader('__forbidden')){
	    		Ext.ux.Toast.msg('系统访问权限提示：','你目前没有权限访问：{0}',options.url);
	    	}
		}
	});
	
	//加载权限
	Ext.Ajax.request({
			url:__ctxPath+'/system/getCurrentAppUser.do?random=' + Math.random(),
			method:'Get',
			success:function(response,options){
				var object=Ext.util.JSON.decode(response.responseText);
				var user=object.user;
				//取得当前登录用户的相关信息，包括权限
				var conf=user.items;
				curUserInfo=new UserInfo(user.userId, user.isResAdmin,user.isOfficeManager,user.username, user.fullname,user.depId,user.depName,user.rights,conf);
				/*var centerPanel=Ext.getCmp('centerTabPanel');
				var homeTab=centerPanel.add(new Ext.Panel({
						title:"title"
				}));
			 	centerPanel.activate(homeTab);*/
			}
	});
	
	//显示应用程序首页
	var indexPage=new IndexPage();
	
	var viewId=getCookie('viewId');
	if (viewId != null && viewId != '' && viewId!='null') {
		App.clickTopTab(viewId);
	}
	
	//启动流程
	var topUrl = new String(window.location);  
	var index = topUrl.indexOf('?');  
	if(index > 0) {
		var urlJSON = Ext.urlDecode(topUrl.substring(index+1));
		if (urlJSON['StartFlow']) {
			var flowDefId = "";
			eval("flowDefId=FlowDefIdPro." + urlJSON['StartFlow']);
			
			if (flowDefId != "") {
				App.clickTopTab("NewProcess", null, null, function(obj){
					
					var contentPanel = App.getContentPanel();
					var startForm = contentPanel.getItem('ProcessRunStart' + flowDefId);
					if (startForm == null) {
						startForm = new ProcessRunStart({
							id : 'ProcessRunStart' + flowDefId,
							defId : flowDefId,
							flowName : "表单填写"
						});
						contentPanel.add(startForm);
					}
					contentPanel.activate(startForm);
					
				});
			}
		}
	}
};

/**
 * 
 * @param {} id
 * @param {} callback 回调函数
 */
App.clickTopTab=function(id,params,precall,callback){
	if(precall!=null){
		precall.call(this);
	}
	var tabs = Ext.getCmp('centerTabPanel');
	var tabItem = tabs.getItem(id);
	
	var _id = id;
	if(_id.match("_")){
		_id = id.substring(0,id.indexOf("_"));
	}

	if (tabItem == null) {
		$ImportJs(_id, function(view) {
			tabItem = tabs.add(view);
			tabs.activate(tabItem);
			if(callback!=null){
				callback.call(this);
			}
		},params);
	}else {
		if(callback!=null){
			callback.call(this);
		}
		tabs.activate(tabItem);
	}
};

App.MyDesktopClickTopTab=function(id,params,precall,callback){
	if(precall!=null){
		precall.call(this);
	}
	var tabs = Ext.getCmp('centerTabPanel');
	var tabItem = tabs.getItem(id);

	if (tabItem == null) {
		if(params.taskName != null){
			if(params.taskName.indexOf('--')>=0){
	  			params.taskName = params.taskName.substring(0,params.taskName.indexOf('--'));
	  		}
			//add by hejianghai
			for(var i=0;i<tabs.items.length;i++){
				if(tabs.getItem(i).title == params.taskName){
					var formView=tabs.getItem(i);
					tabs.activate(formView);
					Ext.ux.Toast.msg("操作提示","请先处理上一个同类型的任务！");
					return;
				}
			}
		}
		$ImportJs(id, function(view) {
			tabItem = tabs.add(view);
			tabs.activate(tabItem);
		},params);
	}else {
		tabs.remove(tabItem);
		var str='new ' + id ;
		if(params!=null){
			str+='(params);';
		}else{
			str+='();';
		}
		var view= eval(str);
		tabItem = tabs.add(view);
		tabs.activate(tabItem);
	}
};

App.clickNode = function(node) {
	if(node.id==null || node.id=='' || node.id.indexOf('xnode')!=-1){
		return ;
	}
//	alert('Ext.decode(node.attributes.params)'+Ext.decode(node.attributes.params));
	App.clickTopTab(node.id,Ext.decode(node.attributes.params));
//	App.clickTopTab(node.id);
};
/**
 * 桌面点击
 */
App.MyDesktopClick=function(){
	var desktopPanel=Ext.getCmp("MyDesktop");
	
//	desktopPanel.expand(true);//点击桌面没反应 byF7400185
	App.clickTopTab('AppHome');
};
/**
 * 退出系统
 */
App.Logout = function() {
	Ext.Ajax.request({
				url : __ctxPath + '/j_logout.do',
				success : function() {
					window.location.href = __ctxPath + '/login.jsp';
				}
	});
};

/*
 * 刷新待办事项(包含首页上的)
 * add by hejianghai
 */
function refreshTaskPanelView(){
	if(Ext.getCmp('TaskPanelView') != null){ //首页
		Ext.getCmp('TaskPanelView').getUpdater().update( __ctxPath + '/flow/displayTask.do?ran=' + Math.random());
	}
	if(Ext.getCmp('MyTaskGrid') != null){ 
		Ext.getCmp('MyTaskGrid').getStore().reload();
	}
}

/*
 * 打开新流程
 * add by hejianghai
 */
function openFlowApplyTab(defId, name){
	var contentPanel = App.getContentPanel();
	var startForm = contentPanel.getItem('ProcessRunStart' + defId);

	if (startForm == null) {
		startForm = new ProcessRunStart({
					id : 'ProcessRunStart' + defId,
					defId : defId,
					flowName : name
				});
		contentPanel.add(startForm);
	}
	contentPanel.activate(startForm);
}
/*
 * 打开父流程审批信息
 * add by lxw
 */
function openParentSPView(runId,defId,piId,name){
	new FlowSendForm({
			runid:runId,
			defid:defId,
			piid:piId,
			name:name
		}).show();		
}
	
//应用程序总入口
Ext.onReady(App.init);
